package com.clm.service.impl;

import com.clm.context.BaseContext;
import com.clm.dto.ShoppingCartDTO;
import com.clm.entity.Dish;
import com.clm.entity.Setmeal;
import com.clm.entity.ShoppingCart;
import com.clm.mapper.DishMapper;
import com.clm.mapper.SetmealMapper;
import com.clm.mapper.ShoppingCartMapper;
import com.clm.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author dinghuapeng
 * @Date 2023/11/6 13:46
 * @PackageName:com.clm.service.impl
 * @ClassName: ShoppingCartServiceImpl
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();//获取当前微信用户的ID
        shoppingCart.setUserId(currentId);
        //判断购物车中的商品是否存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果存在，数量+1
        if(list.size()>0){
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartMapper.update(shoppingCart1);
        }else{
            //判断本次添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId !=null){
                //本次添加购物车的是菜品
                Dish dish=dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                //本次添加的是套餐
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.getByid(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查询购物车
     * @return
     */
    public List showShoppingCart() {
        //获取当前微信用户的id
        Long userId=BaseContext.getCurrentId();
        ShoppingCart build = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(build);
        return list;
    }

    /**
     * 删减商品
     * @param shoppingCartDTO
     */
    @Override
    public void deleteShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();//获取当前微信用户的ID
        //用户id
        shoppingCart.setUserId(currentId);
        //dishid
        shoppingCart.setDishId(shoppingCartDTO.getDishId());
        //口味
        shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());

        //判断数量是否为最后一个
        //将数据取出来
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list.get(0).getNumber()!=1){
            //如果不是，数量减一
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            shoppingCartMapper.update(shoppingCart1);
        }else{
            //如果是的话，直接删除这一条数据
            shoppingCartMapper.deleteOne(shoppingCart);
        }
    }

    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

}
